package m1_simple;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer {
    public static void main(String[] args) throws Exception {
        //1.连接
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140");
        f.setPort(5672);
        f.setUsername("admin");
        f.setPassword("admin");
        Connection con = f.newConnection();
        Channel c = con.createChannel();
        //声明队列,如果该队列已经创建过,则不会重复创建
        //2.定义队列helloworld()  消费者和服务者都要定义队列的
        c.queueDeclare("helloworld",//队列名
                false,
                false,
                false,
                null);

       //收到消息后用来处理消息的回调对象
        DeliverCallback deliverCallback = new DeliverCallback() {
            @Override
            public void handle(String consumerTag, Delivery message) throws IOException {
                byte[] a = message.getBody();
                String msg = new String(a);
                System.out.println("收到：：" + msg);
            }
        };
        //消费者取消时的回调对象
        CancelCallback cancelCallback = new CancelCallback() {
            @Override
            public void handle(String consumerTag) throws IOException {

            }
        };
        //3.消费,接收消息
        c.basicConsume("helloworld",
                true,
                deliverCallback,//处理消息的回调对象
                cancelCallback);//取消接收消息的对象
    }
}
